<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 实现一个电子钟：都是汉字中文 显示年月日 时分秒

1.10-20 之间 十一 、 十二、十三 。。。。

20 以后 二十一 二十二 二十三 ....
3.20,30,40 50... 二十 三十 四十

4.不需要考虑 60以上 -->
    <script>
      function getWord(num) {
        var arr = [
          "零",
          "一",
          "二",
          "三",
          "四",
          "五",
          "六",
          "七",
          "八",
          "九",
          "十",
        ];
        if (0 <= num && num <= 10) {
          return arr[num];
        } else if (10 < num && num < 20) {
          num = num + "";
          return "十" + arr[num[1]];
        } else if (num > 20 && num % 10 != 0) {
          num = num + "";
          return arr[num[0]] + "十" + arr[num[1]];
        } else if (num >= 20 && num % 10 == 0) {
          num = num + "";
          return arr[num[0]] + "十";
        } else {
          throw new Error("输入错误");
        }
      }

      // var res = getWord(40);
      // console.log(res);
      function getTimeFn() {
        var time = new Date();
        var year = fomartYear(time.getFullYear());
        // console.log(year);
        var month = getWord(time.getMonth() + 1);
        var day = getWord(time.getDate());
        var hour = getWord(time.getHours());
        var minute = getWord(time.getMinutes());
        var second = getWord(time.getSeconds());
        var str = `显示时间是  ${year}年${month} 月 ${day} 日 ${hour} 时 ${minute} 分 ${second} 秒`;
        document.body.innerHTML = str;
      }
      getTimeFn();
      setInterval(function () {
        getTimeFn();
      }, 1000);

      // 把年份转换成中文的年份；
      function fomartYear(year) {
        year = year + "";
        var str = "";
        for (var i = 0; i < year.length; i++) {
          str += getWord(year[i]);
        }
        return str;
      }

      console.log(fomartYear(2021));
    </script>
  </body>
</html>
